package org.potiorb.demo.generated;


import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.Reader;
import java.io.Writer;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

import org.potiorb.orb.ORBImpl;
import org.potiorb.orb.giop.GIOPMessageReply;
import org.potiorb.orb.giop.GIOPMessageRequest;
import org.potiorb.orb.invoke.Invoker;
import org.potiorb.orb.marshal.Any;
import org.potiorb.orb.marshal.CallData;
import org.potiorb.orb.marshal.Marshaler;
import org.potiorb.orb.marshal.ParamData;
import org.potiorb.orb.marshal.ParamMode;
import org.potiorb.orb.marshal.TCKind;
import org.potiorb.orb.marshal.TypeCodeImpl;
import org.potiorb.orb.portable.ObjectImpl;
import org.potiorb.orb.portable.ObjectRef;

/**
 *	Generated from IDL definition of interface "Teste"
 *	@author JacORB IDL compiler
 */


public class _TesteStub extends ObjectImpl implements Teste
{
	private String[] ids = {"IDL:Teste:1.0","IDL:omg.org/CORBA/Object:1.0"};
	public String[] _ids()
	{
		return ids;
	}

	public final static java.lang.Class _opsClass = TesteOperations.class;
	public void simples_hello(java.lang.String texto)
	{
		ObjectRef _ref = (ObjectRef)this._get_delegate();

		ParamData _paramslist[] = new ParamData[1];
		ParamData _param = new ParamData();
		_param.setParamMode(new ParamMode(ParamMode._pm_IN));
		_param.setTypeCode(new TypeCodeImpl(TCKind.tk_string));
		_paramslist[0] =_param;

		CallData _call = new CallData();
		_call.setMethodName("simples_hello");
		_call.setNoOfParameters(1);
		_call.setParamerers(_paramslist);

		Any _value_param = new Any();
		_value_param.insert_string(texto);
                ORBImpl _orb = _ref.getOrb();
                Marshaler mar =  _orb.getMarshaler();
		GIOPMessageRequest message = mar.MarshalInvocationRequest(_ref,_call, _value_param);
                Invoker inv = _orb.getInvoker();
                GIOPMessageReply _replymsg = inv.Invoke(message,_ref);
		_ref.getOrb().getMarshaler().UnmarshalInvocationReply(_replymsg);
	}
	
	public void teste(){
		ObjectRef _ref = (ObjectRef)this._get_delegate();
		ORBImpl orb = _ref.getOrb();
		Marshaler mar = orb.getMarshaler();
	}
	
	public String getTimeAsString() {
		
		ObjectRef _ref = (ObjectRef)this._get_delegate();
		
		/**
		 * Atributos para conexão HTTP
		 */
		URL webService = null;
		HttpURLConnection conn = null;
		ByteArrayOutputStream response = new ByteArrayOutputStream();
		
		/**
		 * Criando XML de envio para o servidor
		 */
		StringBuilder send = new StringBuilder();
		send.append("<?xml version=\"1.0\" ?>");
		send.append("<S:Envelope xmlns:S=\"http://schemas.xmlsoap.org/soap/envelope/\">");
		send.append("<S:Body><jb:getTimeAsString xmlns:jb=\"http://exe/\"/></S:Body>");
		send.append("</S:Envelope>");
		String receive = "";
		
		
		/**
		 * Criando URL de conexão
		 */
		try {
			webService = new URL("http://localhost:9876/ts/exe/getTimeAsElapsed");
		} catch (MalformedURLException e) {
			System.out.println("MalformedURLException");
		}
		
		/**
		 * Abrindo conexão e setando parametros da mesma
		 */
		Reader body = new InputStreamReader(new ByteArrayInputStream(send.toString().getBytes()));
		try {
			conn = (HttpURLConnection) webService.openConnection();
			conn.setRequestProperty("Content-type", "text/xml; charset=UTF-8");
			conn.setRequestMethod("POST");
			conn.setDoOutput(true);
			
			OutputStream output = conn.getOutputStream();
			
			Writer writer = new OutputStreamWriter(output);
		} catch (IOException e) {
			System.out.println("Erro de conexão!");
		}
		
		return null;
	}
}
